import Vue from 'vue'
import Vuex from 'vuex'

import app from './modules/app'
import user from './modules/user'
import permission from './modules/permission'
import enhance from './modules/enhance'
import online from './modules/online'
import dp from './modules/dp'
import getters from './getters'
import _ from 'lodash';
import * as api from '../api/gis';
import { message } from 'ant-design-vue';
import { formateTree } from '../utils/module';
import { SPECIAL_EFFECTS } from '@/store/mutation-types'
Vue.use(Vuex)

export default new Vuex.Store({
  modules: {
    app,
    user,
    permission,
    enhance,
    online,
    dp
  },
  state: {
    parks: [], // 勾选的园区
    zIndex: '', // id 顶层图片id
    moudles: {},
    selectMoudles: 'environmental',
    selectLayerKey: '', // 选中的图层
    layers: {}, // c图层
    dPosition: {
      right: 14,
      top: 96
    }, // c区图层定位
    dLyers: {}, // d去图层 会出现重复的图层
    fposition: {
      left: 343,
      top: 94
    }, // D区图层定位
    searchLayer: {
      id: '123search',
      name: 'search',
      position: {
        left: '282px',
        top: '104px'
      }
    }, // 查询特殊图层数据
    specialEffectsErp: true // 特效开关
  },
  mutations: {
    SET_SPECIAL_EFFECTS(state, playod) {
      state.specialEffectsErp = playod;
    },
    SET_PARKID(state, playod) {
      state.parks = playod;
    },
    SET_Z_INDEX(state, playod) {
      state.zIndex = playod;
    },
    SET_MODULES(state, playod) {
      let newObj = _.cloneDeep(state.moudles);
      newObj[playod.key] = playod.data;
      state.moudles = newObj;
    },
    SET_LAYER_KEY(state, playod) {
      state.selectLayerKey = playod;
    },
    ADD_PLAYERS(state, playod) {
      let newObj = _.cloneDeep(state.layers);
      newObj[playod.data.id] = playod;
      state.layers = newObj;
    },
    DELETE_LAYERS(state, playod) {
      if (playod) {
        let newObj = _.cloneDeep(state.layers);
        delete newObj[playod];
        state.layers = newObj;
      } else {
        state.layers = {}
      }
    },
    SET_POSITION(state, playod) {
      state.dPosition = playod;
    },
    ADD_D_PLAYERS(state, playod) {
      let newObj = _.cloneDeep(state.dLyers);
      newObj[playod.key] = playod;
      state.dLyers = newObj;
    },
    DELETE_D_LAYERS(state, playod) {
      if (playod) {
        let newObj = _.cloneDeep(state.dLyers);
        delete newObj[playod];
        state.dLyers = newObj;
      } else {
        state.dLyers = {}
      }
    },
    SET_D_POSITION(state, playod) {
      state.fposition = playod;
    },
    SET_NAV_MODULE(state, playod) {
      state.selectMoudles = playod;
    }
  },
  actions: {
    setSpecialEffects({
      commit
    }, playod) {
      commit('SET_SPECIAL_EFFECTS', playod)
    },
    // 设置园区id
    setParkId({ commit }, playod) {
      commit('SET_PARKID', playod);
    },
    // 设置zIndex层级
    setZIndex({
      commit
    }, playod) {
      commit('SET_Z_INDEX', playod);
    },
    // 设置选中的图层
    setLayerKey({
      commit
    }, playod) {
      commit('SET_LAYER_KEY', playod);
    },
    // 新增图层
    addLayers({
      commit
    }, playod) {
      commit('ADD_PLAYERS', playod)
    },
    // 删除模块
    deleteLayers({
      commit
    }, playod) {
      commit('DELETE_LAYERS', playod);
    },
    // 设置初始定位
    setPosistion({
      commit
    }, playod) {
      commit('SET_POSITION', playod);
    },
    // D区图层添加
    addDLayers({
      commit
    }, playod) {
      commit('ADD_D_PLAYERS', playod);
    },
    // 删除模块
    deleteDLayers({
      commit
    }, playod) {
      commit('DELETE_D_LAYERS', playod);
    },
    // 设置初始定位
    setDPosistion({
      commit
    }, playod) {
      commit('SET_D_POSITION', playod);
    },
    // 切换模块
    setNavModule({
      commit
    }, playod) {
      commit('SET_NAV_MODULE', playod);
    },
    getTreeList({
      dispatch
    }, playod) {
      console.log(playod, 'playod');
      dispatch(`${playod.name}Tree`, {
        name: playod.searchMsg
      });
    },
    environmentalTree({ commit }, playod) {
      const params = {
        name: playod.name
      }
      console.log(params);
      api.getTreeData(params).then((res) => {
        if (res.success) {
          const result = res.result || {};
          const newTree = {
            list: formateTree([result], 'environmental'),
            name: 'environmental'
          }
          commit('SET_MODULES', {
            key: 'environmental',
            data: newTree
          })
        } else {
          message.error(res.message);
        }
      });
    }
  },
  getters
})
